package com.winit.openapi.apiadapter.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.oms.spi.outbound.OutboundOrderTrackingService;
import com.winit.oms.spi.outbound.command.order.QueryOrderTrackingCommand;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderTrackingVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.ups.spi.sys.CustomerTansferConfigService;
import com.winit.ups.spi.warehouse.WarehouseService;

/**
 * 出库单轨迹查询接口适配
 * 老接口地址：<url>http://developer.winit.com.cn/Index/index.php?s=Index/index
 * /id/25/l/zh-cn<url>
 * 
 * @version <pre>
 * Author   Version     Date        Changes
 * minghao.deng     1.0         2016年12月12日     Created
 *
 * </pre>
 * @since 1.
 */
@Controller
public class QueryOrderTrackAdapteController extends AbstractAdapteController {

    @Resource
    private WarehouseService             upsWarehouseService;

    @Resource
    private CustomerTansferConfigService customerTansferConfigService;

    @Resource
    private OutboundOrderTrackingService outboundOrderTrackingService;

    @RequestMapping(value = "/queryTrack")
    public String queryOutboundOrderTrack(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        return doAdapte(requestMsg, responseMsg);
    }

    @Override
    protected void validateInputJson(RequestMsg requestMsg) {
        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        if (null == json) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        ValidateUtil.validMaxLength(json, "outboundNum", 64, false);

        ValidateUtil.validMaxLength(json, "warehouseID", 10, false);
        
        ValidateUtil.validMaxLength(json, "trackingNum", 64, false);
    }

    @Override
    protected boolean checkCustomerTansferConfig(RequestMsg requestMsg, ResponseMsg responseMsg) {
//        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
//        if (json.containsKey("outboundNum")) {
//            String orderNo = json.getString("outboundNum");
//
//            return orderNo.contains("WO");
//        }

        return true;
    }

    @Override
    protected void doAdapteInOpenAPI(RequestMsg requestMsg, ResponseMsg responseMsg) {
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 返回新系统轨迹，新系统轨迹查询单号必填
        ValidateUtil.validNotNull(json, "outboundNum");
        String orderNo = json.getString("outboundNum");
        // 查询订单轨迹
        QueryOrderTrackingCommand command = new QueryOrderTrackingCommand();
        command.setOrderNo(orderNo);
        Page<OutboundOrderTrackingVo> page = outboundOrderTrackingService.getOrderTrackingByOrderNo(command);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        if (page != null) {
            List<OutboundOrderTrackingVo> dataList = page.getContent();
            for (OutboundOrderTrackingVo vo : dataList) {
                Map<String, Object> tempMap = new HashMap<String, Object>();
                tempMap.put("scandateTime", vo.getTrackingTime());
                tempMap.put("trackingmess", vo.getTrackingMsg());
                tempMap.put("scanlocation", vo.getTrackingAddr());
                tempMap.put("status", vo.getTrackingCode());
                mapList.add(tempMap);
            }

        }
        responseMsg.setData(mapList);

    }
}
